package com.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.configuration.Configuration;
import com.user.JSONParser;

public class UserUpdate {
	//URL for fetching data
    private static String updateUserByTelephoneURL = "http://128.199.214.245/android/UserUpdate/update_user_by_telephone.php";

    //Keys for the Json field
    /*
     * KEY_STATUS shows the status of the request
     * 0 : Fail to get user
     * 1 : Successfully in getting user
     */
    private String KEY_STATUS = "STATUS";
    private String KEY_ID = "ID";
    private String KEY_NAME = "NAME";
    private String KEY_DESCRIPTION = "DESCRIPTION";
    private String KEY_PROFILE_PICTURE = "PROFILE_PICTURE";
    
    private String FAIL = "0";
    private String SUCCESS = "1";
    
    //For making httpRequest
    private JSONParser jsonParser;
    private JSONObject result;
    List<NameValuePair> params;
    
    
    //Constructor
    public UserUpdate(){
        jsonParser = new JSONParser();
    }
    
    /*
	 * updateUserByTelephone function will return an User object based on user id entered
	 * - non-null	: Data of the user with id = id
	 * - null		: No such user
	 */
	public int updateUserByTelephone(String telephone,String name, String description){
		//Get Json object
		// Building Parameters
        params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("telephone",telephone));
        params.add(new BasicNameValuePair("name",name));
        params.add(new BasicNameValuePair("description",description));
        
        
        //Start a new thread and use POST method to send telephone and passcode to authURL
        //Start a new thread to attempt Login (requirement when connecting online)
		Thread attemptThread = new Thread(new Runnable(){  
			public void run(){  
				result = jsonParser.makeHttpRequest(updateUserByTelephoneURL,"POST",params);
			}
		});  
		attemptThread.start();
		
		while(attemptThread.isAlive()){
			//Wait for server response
		}
		
		//Parse the json object
		try {
			if(result.getString(KEY_STATUS) != null){
				//Able to get response from server
				if(result.getString(KEY_STATUS).equalsIgnoreCase(SUCCESS)){
					//Successful in finding the user
					if(Configuration.showLog){
						Log.d("UserUpdate","updateUserByTelephone - update ok");
					}
					
					return Integer.parseInt(SUCCESS);
					
				}else{
					//Fail to find such user
					if(Configuration.showLog){
						Log.d("UserUpdate","updateUserByTelephone - update user");
					}
				}
			}else{
				//unable to get response from server
				if(Configuration.showLog){
					Log.d("UserUpdate","updateUserByTelephone - Unable to get response from server");
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			if(Configuration.showLog){
				Log.d("UserUpdate","updateUserByTelephone - Exception");
			}
		}

		return Integer.parseInt(FAIL);
	}
}
